package rabbitmq.topics;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import rabbitmq.constants.Constants;
import rabbitmq.utils.WorkUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 通配符模式
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = WorkUtils.createConnectionFactory();
        //1.建立连接
        Connection connection = factory.newConnection();
        //2.开启信道
        Channel channel = connection.createChannel();
        //3.声明交换机
        channel.exchangeDeclare(Constants.TOPICS_EXCHANGE, BuiltinExchangeType.TOPIC,true,false,false,null);
        //4.声明队列
        channel.queueDeclare(Constants.TOPICS_QUEUE1,true,false,false,null);
        channel.queueDeclare(Constants.TOPICS_QUEUE2,true,false,false,null);
        //5.绑定队列
        channel.queueBind(Constants.TOPICS_QUEUE1,Constants.TOPICS_EXCHANGE,"*.a.*");
        channel.queueBind(Constants.TOPICS_QUEUE2,Constants.TOPICS_EXCHANGE,"*.*.b");
        channel.queueBind(Constants.TOPICS_QUEUE2,Constants.TOPICS_EXCHANGE,"c.#");
        //6.发送消息
        String msg = "hello fanout~";
        channel.basicPublish(Constants.TOPICS_EXCHANGE,"ef.a.b",null,msg.getBytes());
        channel.basicPublish(Constants.TOPICS_EXCHANGE,"c.a.a.a",null,msg.getBytes());
        System.out.println("发送消息成功");
        //7.释放资源
        channel.close();
        connection.close();
    }
}
